package com.doit.day03

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}
import scala.io.Source

object _01_迭代器 {
  def main(args: Array[String]): Unit = {
    val iterator: ScalaIterator = new ScalaIterator
    while (iterator.hasNext){
      println(iterator.next())
    }


    val iterable: ScalaIterable = new ScalaIterable
    for (elem <- iterable) {
      println(elem)
    }

    val iterator1: Iterator[ScalaHero] = iterable.iterator
    while (iterator1.hasNext){
      println(iterator1.next())
    }
  }
}

/*class ScalaIterator extends Iterator[ScalaHero] {
  val lines: Iterator[String] = Source.fromFile("D:\\develop\\ideaWorkSpace\\myself\\doit40\\scala40\\src\\data\\Hero.txt").getLines()
  override def hasNext: Boolean = lines.hasNext
  override def next(): ScalaHero = {
    val line: String = lines.next()
    //1,好似给_100::90
    val arr: Array[String] = line.split(",")
    val arr1: Array[String] = arr(1).split("_")
    val arr2: Array[String] = arr1(1).split("::")
    ScalaHero(arr(0).toInt,arr1(0),arr2(0).toInt,arr2(1).toInt)
  }
}*/
class ScalaIterator extends Iterator[ScalaHero] {

  //mysql
  private val conn: Connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test01", "root", "123456")
  private val pps: PreparedStatement = conn.prepareStatement("select * from user_info")
  private val set: ResultSet = pps.executeQuery()

  override def hasNext: Boolean = set.next()

  override def next(): ScalaHero = {
    val id: Int = set.getInt("id")
    val name: String = set.getString("name")
    val gender: String = set.getString("gender")
    val age: Int = set.getInt("age")
    ScalaHero(id, name, age, gender)
  }
}


class ScalaIterable extends Iterable[ScalaHero] {
  override def iterator: Iterator[ScalaHero] = new ScalaIterator
}

case class ScalaHero(id:Int,name: String,age:Int,gender:String)